/*
 * TCSS 360 - Winter 2014
 * TESK Software
 */
package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import view.userProfile.AuthorView;
import view.userProfile.ProgramChairView;
import view.userProfile.ReviewerView;
import view.userProfile.SubProgramChairView;


import Content.ProjectModel;

/**
 * The view that provides the ability to log in to the system.
 * 
 * @author Khanh Nguyen
 * @version March 20, 2014
 */
@SuppressWarnings("serial")
public class LogInView extends JPanel{

	/** The frame to associate the panel with. */
	private MainFrame my_frame;
	
	/** The text field the user enters their first name into. */
	private JTextField first_text;
	
	/** The text field the user enters their last name into. */
	private JTextField last_text;
	
	/** The project model associated with the program. */
	ProjectModel projectModel;

	/**
	 * Constructs the log in view panel.
	 * 
	 * @param the_frame the frame to associate the panel with
	 */
	public LogInView(final MainFrame the_frame) {
		my_frame = the_frame;
		init();
		
		ImageIcon icon = new ImageIcon(getClass().getResource("/cherry.gif"));
		final JLabel pic = new JLabel();
		pic.setIcon(icon);
	//	final JLabel pic = new JLabel();
		//pic.setIcon(new ImageIcon("..\\Conference System\\src\\cherry.gif"));
		this.add(pic);
	}

	/**
	 * Provides the specifications and adds components of the panel. 
	 */
	private void init( ){
		this.setBackground(new Color(255, 240, 245));
		//icon label
		
		ImageIcon icon = new ImageIcon(getClass().getResource("/border.jpg"));
		final JLabel pic = new JLabel();
		pic.setIcon(icon);
		//pic.setIcon(new ImageIcon("..\\Conference System\\src\\border.jpg"));
		this.add(pic);
		this.add(usernamePanel());
		


	}
	
	/**
	 * Creates the panel that contains the buttons.
	 * @return panel.
	 * @author Khanh Nguyen, Thang Tran, Emily Linniman
	 */
	private JPanel buttonPanel() {
		final JPanel bg_panel = new JPanel();
		bg_panel.setOpaque(false);

		JButton login = new JButton("LogIn");
		login.setForeground(Color.blue);
	
		login.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent arg0) {

				String firstName = first_text.getText();
				String role = my_frame.db.getRole(firstName);

				my_frame.currentUser[0][0] = firstName;
				my_frame.currentUser[0][1] = role;
				
				if(role.equals("author")) {
					my_frame.setNewPanel(new AuthorView(my_frame));
					
				} else if(role.equals("reviewer")) {
					my_frame.setNewPanel(new ReviewerView(my_frame));
					
				} else if(role.equals("subChair")) {
					my_frame.setNewPanel(new SubProgramChairView(my_frame));
					
				} else if(role.equals("programChair")) {
					my_frame.setNewPanel(new ProgramChairView(my_frame));
					
				} else {
					my_frame.setNewPanel(new AuthorView(my_frame));
				}
			}
		});

		bg_panel.add(login);

		return bg_panel;

	}
	
	/**
	 * Top panel displays username label, username text,
	 * and password label, password textbox.
	 * @return Panel the username panel
	 * @author Khanh
	 */
	private JPanel usernamePanel(){

		final JPanel panel = new JPanel();// panel contain username pw text.
		panel.setOpaque(false);
		
		
		panel.setPreferredSize(new Dimension(250,150));
		final JLabel user_label = new JLabel ("First Name: ");
		
		final JLabel pass_label = new JLabel ("Last Name: ");
		

		first_text = new JTextField(20);first_text.setToolTipText("First name is case sensitive");
		last_text = new JTextField(20);last_text.setToolTipText("Last name is case sensitive");

		panel.add(user_label);
		panel.add(first_text);
		panel.add(pass_label);
		panel.add(last_text);

		final JPanel panel1 = new JPanel();//panel below "panel" - username pw text.
		panel1.add(panel);
		panel1.setOpaque(false);

		final JPanel basePanel = new JPanel(new GridLayout(2,0));//an empty panel to put under 2 panels.
		basePanel.setBackground(new Color(135, 206, 235));

		basePanel.add(panel1);
		basePanel.add(buttonPanel());
		basePanel.setPreferredSize(new Dimension(500,220));

		return basePanel;
	}
}

//online example about log in and out 
//http://superliminal.com/sources/LoginPanel.java.html